/**
 * Project Name:springBoot-controller
 * File Name:RedisController.java
 * Package Name:com.libra.controller
 * Date:2018年8月11日下午2:23:55
 * Copyright (c) 2018, wangzhch All Rights Reserved.
 *
*/

package com.libra.controller;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.libra.model.redis.Student;
import com.libra.model.vo.ResponseData;
import com.libra.serviceImp.RedisServiceImp;

/**
 * ClassName:RedisController <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason:	 TODO ADD REASON. <br/>
 * Date:     2018年8月11日 下午2:23:55 <br/>
 * @author   wangzhch
 * @version  
 * @since    JDK 1.8
 * @see 	 
 */
@RestController
@RequestMapping("redis")
public class RedisController {
	Logger log = LoggerFactory.getLogger(RedisController.class);
	@Autowired
	private RedisServiceImp redisServiceImp;
	
	/**
	 * 
	 * writeCache:(写入缓存). <br/>
	 *
	 * @author wangzhch
	 * @param student
	 * @return
	 * @since JDK 1.8
	 */
	@RequestMapping(value = "/writeCache",method = RequestMethod.POST)
    public ResponseData writeCache(@RequestBody Student student){
		log.info("writeCache接口请求参数打印:{}",JSON.toJSONString(student));
		ResponseData responseData = redisServiceImp.writeCache(String.valueOf(student.getId()),student); //存入的值为一个对象
//		ResponseData responseData = redisServiceImp.writeCache(String.valueOf(student.getId()),student.getName());
		log.info("writeCache接口响应参数打印:{}",JSON.toJSONString(responseData));
		return responseData;
    }
	
	/**
	 * 
	 * wrightCaheByExpireTime:(写入缓存设置时效时间,存入的值为一个集合). <br/>
	 *
	 * @author wangzhch
	 * @param student
	 * @return
	 * @since JDK 1.8
	 */
	@RequestMapping(value = "/writeCaheByExpireTime", method = RequestMethod.POST)
	public ResponseData wrightCaheByExpireTime(@RequestBody Student student) {
		log.info("writeCaheByExpireTimeji二口请求参数:{}",JSON.toJSONString(student));
		List<Object> list = new ArrayList<>();
		list.add(student.getId());
		list.add(student.getAge());
		list.add(student.getName());
		list.add(student.getSex());
		list.add(student.getExpireTime());
		list.add(student.getScore());
		//注意:这里的失效时间是秒为单位
		ResponseData responseData = redisServiceImp.wrightCaheByExpireTime(String.valueOf(student.getId()), list, student.getExpireTime());
		log.info("writeCaheByExpireTime接口响应参数:{}",JSON.toJSONString(responseData));
		return responseData;
	}
	/**
	 * 
	 * deleteBatchValue:(批量删除key对应的value). <br/>
	 *
	 * @author wangzhch
	 * @param list
	 * @return
	 * @since JDK 1.8
	 */
	@RequestMapping(value = "/deleteBatchValue" , method = RequestMethod.POST)
	public ResponseData deleteBatchValue(@RequestBody List<String> list) {
		log.info("deleteBatchValue接口请求参数:{}",JSON.toJSONString(list));
		String[] strings = new String[list.size()];
		list.toArray(strings);
		ResponseData responseData = redisServiceImp.deleteBatchValue(strings);
		log.info("deleteBatchValue接口请求参数:{}",JSON.toJSONString(responseData));
		return responseData;
	}
	
	/**
	 * 
	 * deleteBatchKeys:(批量删除key,key为模糊查询的值:需带有*号,如  1*). <br/>
	 *
	 * @author wangzhch
	 * @param patten
	 * @return
	 * @since JDK 1.8
	 */
	@RequestMapping(value = "/deleteBatchKeys" , method = RequestMethod.POST)
	public ResponseData deleteBatchKeys(@RequestBody String patten) {
		log.info("deleteBatchKeys接口请求参数:{}",JSON.toJSONString(patten));
		ResponseData responseData = redisServiceImp.deleteBatchKeys(patten);
		log.info("deleteBatchKeys接口响应参数:{}",JSON.toJSONString(responseData));
		return responseData;
	}
	/**
	 * 
	 * getCaheValue:(读取缓存). <br/>
	 * @author wangzhch
	 * @param key
	 * @return
	 * @since JDK 1.8
	 */
	@RequestMapping(value = "/getCaheValue" , method = RequestMethod.POST)
	public ResponseData getCaheValue(@RequestBody String key) {
		log.info("getCaheValue接口请求参数:{}",JSON.toJSONString(key));
		ResponseData responseData = redisServiceImp.getCaheValue(key);
		log.info("getCaheValue接口响应参数:{}",responseData);
		return responseData;
	}
	/**
	 * 
	 * addHashPut:(hash添加单个,如果hashKey存在,则修改hashValue). <br/>
	 * 
	 * key和hash的关系是一对多
	 * key对应有多个hash(hashkey,hashValue)
	 * 
	 * @author wangzhch
	 * @param student
	 * @return
	 * @since JDK 1.8
	 */
	@RequestMapping(value = "/addHashPut" , method = RequestMethod.POST)
	public ResponseData addHashPut(@RequestBody Student student) {
		log.info("addHashPut接口请求参数:{}",JSON.toJSONString(student));
		ResponseData responseData = redisServiceImp.addHashPut("n2", student);
		log.info("addHashPut接口响应参数:{}",JSON.toJSONString(responseData));
		return responseData;
	}
	/**
	 * 
	 * getHashValue:(hashkey查询hashValue). <br/>
	 * 根据主key下的hashkey去查询value
	 * @author wangzhch
	 * @param student
	 * @return
	 * @since JDK 1.8
	 */
	@RequestMapping(value = "/getHashValue" , method = RequestMethod.POST)
	public ResponseData getHashValue(@RequestBody Student student) {
		log.info("getHashValue接口请求参数:{}",JSON.toJSONString(student));
		ResponseData responseData = redisServiceImp.getHashValue("n2", student);
		log.info("getHashValue接口响应参数:{}",JSON.toJSONString(responseData));
		return responseData;
	}
	/**
	 * 
	 * addList:(list数据类型添加). <br/>
	 *
	 *	list：有序(先加在前)，元素可重复，可用作队列
	 * @author wangzhch
	 * @param list
	 * @return
	 * @since JDK 1.8
	 */
	@RequestMapping(value = "/addList" , method = RequestMethod.POST)
	public ResponseData addList(@RequestBody List<Student> list) {
		log.info("addList接口请求参数:{}",JSON.toJSONString(list));
		ResponseData responseData = redisServiceImp.addList("list", list);
		log.info("addList接口响应参数:{}",JSON.toJSONString(responseData));
		return responseData;
	}
	/**
	 * 
	 * selectList:(list数据查询). <br/>
	 *	list：有序(先加在前)，元素可重复，可用作队列
	 * @author wangzhch
	 * @param key
	 * @return
	 * @since JDK 1.8
	 */
	@RequestMapping(value = "/selectList" , method = RequestMethod.POST)
	public ResponseData selectList(@RequestBody String key) {
		log.info("selectList接口请求参数:{}",key);
		ResponseData responseData = redisServiceImp.selectList(key);
		log.info("selectList接口响应参数:{}",JSON.toJSONString(responseData));
		return responseData;
	}
	/**
	 * 
	 * addSet:(set集合添加). <br/>
	 * TODO(set：无规则排序，元素唯一不可重复，可用于去重).<br/>
	 *
	 * @author wangzhch
	 * @param list
	 * @return
	 * @since JDK 1.8
	 */
	@RequestMapping(value="/addSet" , method = RequestMethod.POST)
	public ResponseData addSet(@RequestBody List<Student> list) {
		log.info("addSet接口请求参数:{}",JSON.toJSONString(list));
		ResponseData responseData = redisServiceImp.addSet("set", list);
		log.info("addSet接口响应参数:{}",JSON.toJSONString(responseData));
		return responseData;
	}
	/**
	 * 
	 * selectSet:(Set集合查询). <br/>
	 * TODO(set：无规则排序，元素唯一不可重复，可用于去重).<br/>
	 *
	 * @author wangzhch
	 * @param key
	 * @return
	 * @since JDK 1.8
	 */
	@RequestMapping(value = "/selectSet" , method = RequestMethod.POST)
	public ResponseData selectSet(@RequestBody String key) {
		log.info("selectSet接口请求参数:{}",key);
		ResponseData responseData = redisServiceImp.selectSet(key);
		log.info("selectSet接口响应参数:{}",JSON.toJSONString(responseData));
		return responseData;
	}
	/**
	 * 
	 * addZset:(Zset有序集合添加). <br/>
	 * TODO(Zset：有规则排序，元素唯一不可重复，可用于去重).<br/>
	 *
	 * @author wangzhch
	 * @param list
	 * @return
	 * @since JDK 1.8
	 */
	@RequestMapping(value = "/addZset" , method = RequestMethod.POST)
	public ResponseData addZset(@RequestBody List<Student> list) {
		log.info("addZset接口请求参数:{}",JSON.toJSONString(list));
		ResponseData responseData = redisServiceImp.addZset("Zset",list);
		log.info("addZset接口响应参数:{}",JSON.toJSONString(responseData));
		return responseData;
	}
	/**
	 * 
	 * selectZset:(Zset集合获取). <br/>
	 * TODO(Zset：有规则排序，元素唯一不可重复，可用于去重).<br/>
	 *
	 * @author wangzhch
	 * @param key
	 * @return
	 * @since JDK 1.8
	 */
	@RequestMapping(value = "/selectZset" , method = RequestMethod.POST)
	public ResponseData selectZset(@RequestBody String key) {
		log.info("selectZset接口请求参数:{}",key);
		ResponseData responseData = redisServiceImp.selectZset(key);
		log.info("selectZset接口响应参数:{}",JSON.toJSONString(responseData));
		return responseData;
	}
	public static void main(String[] args) {
		List<Student> list = new ArrayList<>();
		Student student = new Student();
		student.setId(1);
		student.setAge("12");
		student.setName("张三");
		student.setSex("1");
		list.add(student);
		
		Student student2 = new Student();
		student2.setId(2);
		student2.setAge("13");
		student2.setName("张三2");
		student2.setSex("2");
		list.add(student2);
		String str = JSON.toJSONString(list);
		System.out.println(str);
		
		Student student3 = new Student();
		student3.setId(2);
		student3.setAge("13");
		student3.setName("张三3");
		student3.setSex("2");
		list.add(student3);
		String strs = JSON.toJSONString(list);
		System.out.println(strs); 
		
		System.out.println(1111);
		System.out.println(2222);
	}
}

